﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using XWG.Framework.AuthUtility;
using XWG.Framework.AuthBLL;
using System.Data;
using XWG.Framework.AuthUtility;
using XWG.Framework.AuthSPConst;
using System.Text.RegularExpressions;
using JWBSTAPP.CommonClass;
using XWG.JWBST.JWBSTSPConst;
public partial class login : System.Web.UI.Page
{

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            //检测cookie 自动登录

        }
    }
    /// <summary>
    /// 管理员登陆
    /// </summary>
    /// <param name="_adminloginname">登录名。</param>
    /// <param name="低调">操作员。</param>
    /// <returns>是否成功 1 表示成功 负值代表失败</returns>
    protected void ButLogin_Click(object sender, EventArgs e)
    {

        XWG.Framework.AuthEntity.AppOrganizatiOnUser mUser = new XWG.Framework.AuthEntity.AppOrganizatiOnUser();
        try
        {
            //获取领域ID

            string _result = "";

            XWG.Framework.AuthEntity.AppLogLogin logLogin = new XWG.Framework.AuthEntity.AppLogLogin();
            _result = Convert.ToString(AppOrganizatiOnUserBLL.Login(username.Value.Trim(), password.Value));
            //_result = Convert.ToString(AppOrganizatiOnUserBLL.Login("ceshi", "111111"));
            if (Succeed(_result))//成功后操作
            {

                //载入用户信息
                mUser = AppOrganizatiOnUserBLL.Load(_result);
                //session赋值
                Session["UserID"] = mUser.AppuserId;
                Session["Orgna_ID"] = mUser.OrgnaId;
                var UerCode = XWG.Framework.AuthBLL.AppOrganizatiOnBLL.Load(Session["Orgna_ID"].ToString());
                Session["UserCode"] = UerCode.Bz0.ToString();
                //session对象
                // Session["User"] = mUser;
                //默认领域ID
                JWBSTAPP.Global.AreaID = XWG.Framework.AuthBLL.AppOrganizatiOnUserBLL.GetAreaID();
                //写入登录日志
                logLogin.Id = System.Guid.NewGuid().ToString();
                logLogin.Ip = AnyString.IPClient();
                logLogin.Loginclient = "";
                logLogin.Logintime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
                logLogin.Logintype = "密码登录";
                logLogin.UserType = Convert.ToInt32(UserType.User);
                logLogin.UserId = _result;
                AppLogLoginBLL.Insert(logLogin);
                //跳转             
                ScriptManager.RegisterStartupScript(ButLogin, this.GetType(), "javascript", "location.href='Main/index.aspx';", true);
            }
            else//失败后操作
            {
                ScriptManager.RegisterStartupScript(ButLogin, this.GetType(), "javascript", "ymPrompt.errorInfo('登录失败！',null,null,null,null);", true);
                return;
            }
        }
        catch (Exception ex)
        {
            //提示信息

            //写入错误日志
            XWG.Framework.MSException.MSEx.DealErr(ex, "");
        }
    }

    #region 验证函数

    /// <summary>
    ///  处理调用方法后的结果
    /// </summary>
    /// <param name="result">需要处理的信息代码</param>
    public bool Succeed(string result)
    {
        if (!string.IsNullOrEmpty(result))
        {
            if (result == "1")
            {
                return true;
            }
            else
            {
                try
                {
                    if (Convert.ToInt32(result) < 0)
                    {
                        return false;
                    }
                    else
                    {
                        return true;
                    }
                }
                catch
                {
                    if (IsGUID(result))
                        return true;
                    else
                        return false;
                }

            }
        }
        return false;
    }
    public bool IsGUID(string str)
    {
        Match m = Regex.Match(str, @"^[0-9a-f]{8}(-[0-9a-f]{4}){3}-[0-9a-f]{12}$", RegexOptions.IgnoreCase);
        if (m.Success)
        {
            //可以转换 
            //Guid guid = new Guid(str);
            return true;
        }
        else
        {
            //不可转换 
            return false;
        }
    }
    #endregion

}